package MovieGuessingGame.confirmRegistration;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

import MovieGuessingGame.Database.PlayerDB;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class ConfirmSSLWorker extends Thread {

	BufferedReader bufferedreader = null;
	SSLServerSocket sslserversocket = null;
	SSLSocket sslsocket = null;
	String userName = null;
	String regCode  = null;
	String password = null;
	String dbPass = null;

	public ConfirmSSLWorker(ConfirmServerSSL sslServer, SSLSocket socket, String dbpass) {
		sslsocket = socket;
		dbPass = dbpass;
		System.out.println("ConfirmSSLWorker");
	}

	void init() throws IOException {

		InputStream inputstream = sslsocket.getInputStream();
		InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
		bufferedreader = new BufferedReader(inputstreamreader);
	}

	void answer(String retStr) throws IOException {
		BufferedWriter bufferedwriter = null;
		OutputStream outputstream = sslsocket.getOutputStream();
		OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
				outputstream);
		bufferedwriter = new BufferedWriter(outputstreamwriter);

		if ("OK".equals(retStr)) {
			try {
				byte[] hash =  getHash(userName);
				/** outputstream.write(hash); **/
				PlayerDB playerdb = new PlayerDB(dbPass);
				playerdb.insertAndUpdatePlayerRecord(true, userName, hash, 0, 0, false); 
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				bufferedwriter.write("Unknown error.");
			}
			bufferedwriter.write(retStr);
		} else {
			bufferedwriter.write(retStr);
		}
		bufferedwriter.flush();
		System.out.println("wrote " + retStr);
	}

	void getUserRegCode() throws IOException {
		String dataString = null;
		boolean ok = true;
		try {
			dataString = bufferedreader.readLine();
		} catch (Exception e) {
			ok = false;
		}
		if (ok) {
			System.out.println(dataString);
			int index = dataString.indexOf(",");
			String[] data = dataString.split(",");
			//userName = string.substring(0, index);
			//regCode = string.substring(index + 1);
			userName = data[0];
			regCode  = data[1];
			password = data[2];
			
			// System.out.println("The user is: " + userName + " - The regCode is: " + regCode + " and the password is: " + password);
		}
	}

	String verifyUserCanChangePassword() {
		ConfirmDB logindn = new ConfirmDB();
		String retStr = logindn.confirmRegistration(userName, regCode, password, dbPass);
		return retStr;
	} 

	public byte[] getHash(String string) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		MessageDigest digest = MessageDigest.getInstance("SHA-256");
		digest.reset();
		byte[] bytesArray = digest.digest(string.getBytes("UTF-8"));
		for (int i = 0;i<bytesArray.length;i++)
			System.out.println(bytesArray[i]);

		return bytesArray;
	}

	/** The run method of this thread. **/
	public void run() {
		try {
			init();
			getUserRegCode();
			answer(verifyUserCanChangePassword());
		} catch (IOException e) {
			System.out.println("Player died: " + e);
		} finally {
			try {
				sslsocket.close();
			} catch (IOException e) {
			}
		}
	}

	public static void main(String[] arstring) {

	}
}